/**
 * Copyright (c) 2014-2015, Suredy technology Co., Ltd. All rights reserved.
 * @author ZhangMaoren 
 * @since 2015年6月26日
 * @version 0.1
 */
package com.suredy.base.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.suredy.base.model.Menu;
import com.suredy.base.model.MenuTreeModel;
import com.suredy.core.service.BaseSrvWithEntity;



/**
 * @author ZhangMaoren
 *
 */
@Service("MenuSrv")
public class MenuSrv extends BaseSrvWithEntity<Menu> {

	public DetachedCriteria getDc(Menu t) {
		DetachedCriteria dc = super.getDc(t);
		if (t == null) {
			return dc;
		}
		if (!StringUtils.isBlank(t.getId())) {
			dc.add(Restrictions.eq("id", t.getId()));
		}
		if (!StringUtils.isBlank(t.getUrl())) {
			dc.add(Restrictions.eq("url", t.getUrl()));
		}
		if (!StringUtils.isBlank(t.getText())) {
			dc.add(Restrictions.like("text", t.getText(), MatchMode.ANYWHERE));
		}
		
		if (!StringUtils.isBlank(t.getResourceId())) {
			dc.add(Restrictions.eq("resourceId", t.getResourceId()));
		}
		
		if (t.getParent() != null) {
			dc.add(Restrictions.eq("parent", t.getParent()));
		}

		return dc;
	}
	
	public Menu getByName(String name) {
		Menu search = new Menu();
		if (!StringUtils.isBlank(name)) {
			search.setText(name);
		}
		search.setParent(null);
		return this.readSingleByEntity(search);
	}
	
	public Menu getByName(String parentId, String name) {
		Menu search = new Menu();
		if (StringUtils.isBlank(parentId) || StringUtils.isBlank(name)) {
			throw new IllegalArgumentException("Invalid parameter String[parentId|name].");
		}
		Menu parent = new Menu();
		parent.setId(parentId);
		search.setParent(parent);
		search.setText(name);
		return this.readSingleByEntity(search);
	}
	
	public List<Menu> getByResource(String resourceId) {
		Menu search = new Menu();
		if (!StringUtils.isBlank(resourceId)) {
			search.setResourceId(resourceId);
		}
		return this.readByEntity(search);
	}

	@Transactional
	public List<MenuTreeModel> getMenuTree(String rootId) {
		String ql = "SELECT T FROM Menu T";

		if (StringUtils.isBlank(rootId)) {
			ql += " WHERE T.parent IS NULL";
		} else {
			ql += " WHERE T.parent = '" + rootId + "'";
		}
		@SuppressWarnings("unchecked")
		List<Menu> menues = (List<Menu>) this.readByQL(ql);
		
		return MenuTreeModel.makeTree(menues);
	}
	
	@Transactional
	public List<MenuTreeModel> getUserMenuTree(String rootId, List<String> allowedMenues) {
		String ql = "SELECT T FROM Menu T";

		if (StringUtils.isBlank(rootId)) {
			ql += " WHERE T.parent IS NULL";
		} else {
			ql += " WHERE T.parent = '" + rootId + "'";
		}
		@SuppressWarnings("unchecked")
		List<Menu> menues = (List<Menu>) this.readByQL(ql);
		
		List<MenuTreeModel> data = MenuTreeModel.makeTree(menues, allowedMenues);
		return data;
	}
	

	/**
	 * @param menuId
	 * @return
	 */
	@Transactional
	public MenuTreeModel getMenuTreeNode(String menuId) {
		if (StringUtils.isBlank(menuId)) {
			throw new IllegalArgumentException("Invalid parameter String[menuId].");
		}

		Menu menu = this.get(menuId);

		if (menu == null) {
			return null;
		}

		return MenuTreeModel.makeNode(menu);
	}

}
